#include"test_redis.h"
#include"CLocalIniParam.h"

 redis_test::redis_test(){

 }
bool redis_test::LoadLocalIni(){
    if (false == CLocalIniParam::GetInstance()->ReadRedisParams("redis_config.ini")){
		return false;
	}

	m_RedisContext.c0Redis.SetConnectParam(CLocalIniParam::GetInstance()->GetRedisConfig(0));
	if (m_RedisContext.c0Redis.Connect()==0) {
		return false;
	}
    m_RedisContext.c1Redis.SetConnectParam(CLocalIniParam::GetInstance()->GetRedisConfig(1));
	if (m_RedisContext.c1Redis.Connect()==0) {
		return false;
	}
    m_RedisContext.c2Redis.SetConnectParam(CLocalIniParam::GetInstance()->GetRedisConfig(2));
	if (m_RedisContext.c2Redis.Connect()==0) {
		return false;
	}
    m_RedisContext.c3Redis.SetConnectParam(CLocalIniParam::GetInstance()->GetRedisConfig(3));
	if (m_RedisContext.c3Redis.Connect()==0) {
		return false;
	}
    m_RedisContext.c4Redis.SetConnectParam(CLocalIniParam::GetInstance()->GetRedisConfig(4));
	if (m_RedisContext.c4Redis.Connect()==0) {
		return false;
	}
    m_RedisContext.c5Redis.SetConnectParam(CLocalIniParam::GetInstance()->GetRedisConfig(5));
	if (m_RedisContext.c5Redis.Connect()==0) {
		return false;
	}
    m_RedisContext.c6Redis.SetConnectParam(CLocalIniParam::GetInstance()->GetRedisConfig(6));
	if (m_RedisContext.c6Redis.Connect()==0) {
		return false;
	}

}

void redis_test::redis_string_test()
{
    //redis key
    std::string strKey="key";
    std::string strField1="strField1";
    std::string strValue1="strValue1";
    std::string strField2="strField2";
    std::string strValue2="strValue2";
    std::string strField3="strField3";
    std::string strValue3="strValue3";
    //redis H_MSet
    CJS_STR_MAP_KEY_VALUE MSetMap;
    MSetMap.insert(CJS_STR_MAP_KEY_VALUE::value_type(strField1,strValue1));
    MSetMap.insert(CJS_STR_MAP_KEY_VALUE::value_type(strField2,strValue2));
    MSetMap.insert(CJS_STR_MAP_KEY_VALUE::value_type(strField3,strValue3));
    m_RedisContext.c6Redis.H_MSet(strKey,MSetMap);
    //redis H_MSet
    CJS_STR_MAP_KEY_VALUE MGetMap;
    MGetMap.insert(CJS_STR_MAP_KEY_VALUE::value_type(strField1,""));
    MGetMap.insert(CJS_STR_MAP_KEY_VALUE::value_type(strField2,""));
    MGetMap.insert(CJS_STR_MAP_KEY_VALUE::value_type(strField3,""));
    m_RedisContext.c6Redis.H_MGet(strKey,MGetMap);
    CJS_STR_MAP_KEY_VALUE::const_iterator MGetBg=MGetMap.begin();
    CJS_STR_MAP_KEY_VALUE::const_iterator MGetEd=MGetMap.end();
    for(;MGetBg!=MGetEd;++MGetBg){
        cout<<"MGetBg->first="<<MGetBg->first<<"\tMGetBg->second="<<MGetBg->second<<endl;
    }
    //redis H_GetAll
    CJS_STR_MAP_KEY_VALUE HGetAllMap;
    m_RedisContext.c6Redis.H_GetAll(strKey,HGetAllMap);
    CJS_STR_MAP_KEY_VALUE::const_iterator MGetAllBg=MGetMap.begin();
    CJS_STR_MAP_KEY_VALUE::const_iterator MGetAllEd=MGetMap.end();
    for(;MGetAllBg!=MGetAllEd;++MGetAllBg){
        cout<<"MGetAllBg->first="<<MGetAllBg->first<<"\tMGetAllBg->second="<<MGetAllBg->second<<endl;
    }
    //redis H_Exists
    bool bExist;
    m_RedisContext.c6Redis.H_Exists(strKey,"strField1",bExist);
    cout<<"bExist="<<bExist<<endl;
    m_RedisContext.c6Redis.H_Exists(strKey,"ss",bExist);

    cout<<endl;
}
void redis_test::redis_list_test(){

    std::string strKey="key1";
    std::string strValue1="strValue1";
    std::string strValue2="strValue2";
    std::string strValue3="strValue3";
    std::string strField1="strField1";
    std::string strField2="strField2";
    std::string strField3="strField3";

    string strOut="";
    m_RedisContext.c6Redis.L_Push(strKey,strValue1);
    m_RedisContext.c6Redis.L_Push(strKey,strValue2);
    m_RedisContext.c6Redis.L_Push(strKey,strValue3);
    m_RedisContext.c6Redis.BR_POP(strKey,0,strOut);

    cout<<"strOut="<<strOut<<endl;

    cout<<endl;
}
void redis_test::redis_sset_test(){
    std::string strKey="setkey";
    std::string strValue1="strValue1";
    std::string strValue2="strValue2";
    std::string strValue3="strValue3";
    std::string strValue4="strValue4";
    std::string strValue5="strValue5";
    int nScore1=10;
    int nScore2=20;
    int nScore3=30;
    int nScore4=40;
    int nScore5=10;
    int nScore=0;
    //zadd
    m_RedisContext.c6Redis.Z_Add(strKey,nScore1,strValue1);
    m_RedisContext.c6Redis.Z_Add(strKey,nScore2,strValue2);
    m_RedisContext.c6Redis.Z_Add(strKey,nScore3,strValue3);
    m_RedisContext.c6Redis.Z_Add(strKey,nScore4,strValue4);
    m_RedisContext.c6Redis.Z_Add(strKey,nScore5,strValue5);
    //zrem
    m_RedisContext.c6Redis.Z_Rem(strKey,strValue1);
    //zscore
    m_RedisContext.c6Redis.Z_Score(strKey,strValue5,nScore);
    cout<<"strValue5 score="<<nScore<<endl;
    //zrevrank
    int nRank=-1;
    m_RedisContext.c6Redis.Z_Revrank(strKey,strValue2,nRank);
    cout<<"nRank="<<nRank<<endl;

    //zrange
    int nStart=0;
    int nStop=6;
    typedef vector<std::string> zVec;
    zVec vec_range;
    m_RedisContext.c6Redis.Z_Range(strKey,nStart,nStop,vec_range);
    cout<<"vecRange.size="<<vec_range.size()<<endl;   

    //ZRANGEBYSCORE
    int nScoStart=10;
    int nScoStop=20;
    zVec vec_scoreRange;
    m_RedisContext.c6Redis.Z_RangeByScore(strKey,nScoStart,nScoStop,vec_scoreRange);
    cout<<"vec_scoreRange.size="<<vec_scoreRange.size()<<endl;

    //zincrby
    int nIncyby=100;
    m_RedisContext.c6Redis.Z_Incrby(strKey,nIncyby,strValue2);
    m_RedisContext.c6Redis.Z_Score(strKey,strValue2,nScore);
    cout<<"strValue2 score="<<nScore<<endl;

}